package com.fmyd.qizhuazhua.http;

import android.text.TextUtils;

import com.fmyd.qizhuazhua.utils.Md5Util;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.ResponseHandlerInterface;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * Created by Kirito
 */
public class HttpApi {

    private static AsyncHttpClient mClient;

    public static AsyncHttpClient getHttpClient() {
        if (mClient == null) {
            mClient = new AsyncHttpClient();
            mClient.setMaxRetriesAndTimeout(2, 10000);
            mClient.setTimeout(15 * 1000);
        }
        return mClient;
    }

    /**
     * <post 泛型统一方法>
     *
     * @param url
     */
    protected static void onHttpGet(String url, RequestParams params, ResponseHandlerInterface l) {
        getHttpClient().get(url, params, l);
    }

    public static void onHttpGet(String url, ResponseHandlerInterface l) {
        getHttpClient().get(url, l);
    }

    public static void onHttpPost(String url, String header, RequestParams params, ResponseHandlerInterface l) {
        getHttpClient().post(url, params, l);
    }

    public static void onHttpPost(String url, RqParams params, ResponseHandlerInterface l) {
        getHttpClient().post(url, params, l);
    }

    public static void onHttpPostSign(String url, RqParams params, ResponseHandlerInterface l) {
        getHttpClient().post(url, sign(params), l);
    }

    public static void onHttpPost(String url, ResponseHandlerInterface l) {
        getHttpClient().post(url, l);
    }

    public static RqParams sign(RqParams params) {
        Map<String, String> map = params.map;
        List<String> list = new ArrayList<>();
        for (String str : map.keySet()) {
            list.add(str);
        }
        String[] paramNames = new String[list.size()];
        paramNames = list.toArray(paramNames);
        Arrays.sort(paramNames);
        String sign = GSonRequest.xxxx;
        for (String paramName : paramNames) {
            if (!TextUtils.isEmpty(map.get(paramName))) {
                sign += paramName + map.get(paramName);
            }
        }
        sign += GSonRequest.xxxx;
        try {
            sign = Md5Util.md5(sign);
            params.put("sign", sign);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return params;
    }

    public static String signString(RqParams params) {
        Map<String, String> map = params.map;
        List<String> list = new ArrayList<>();
        for (String str : map.keySet()) {
            list.add(str);
        }
        String[] paramNames = new String[list.size()];
        paramNames = list.toArray(paramNames);
        Arrays.sort(paramNames);
        String sign = GSonRequest.xxxx;
        for (String paramName : paramNames) {
            if (!TextUtils.isEmpty(map.get(paramName))) {
                sign += paramName + map.get(paramName);
            }
        }
        sign += GSonRequest.xxxx;
        try {
            sign = Md5Util.md5(sign);
            params.put("sign", sign);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sign;
    }
}
